package com.image.fresco;

import android.widget.AbsListView;

import androidx.recyclerview.widget.RecyclerView;

import com.facebook.drawee.backends.pipeline.Fresco;

/**
 * @author qinbaowei
 * @createtime 2017/04/07 13:07
 * @email qbaowei@qq.com
 * @description
 */


public class PauseOnScrollListener extends RecyclerView.OnScrollListener implements AbsListView.OnScrollListener {

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://空闲状态
                imageResume();
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滚动状态
                imagePause();
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://触摸后滚动
                imageResume();
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view,
                         int firstVisibleItem,
                         int visibleItemCount,
                         int totalItemCount) {

    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        switch (newState) {
            case RecyclerView.SCROLL_STATE_IDLE://空闲状态
                imageResume();
                break;
            case RecyclerView.SCROLL_STATE_SETTLING://滚动状态
                imagePause();
                break;
            case RecyclerView.SCROLL_STATE_DRAGGING://触摸后滚动
                imageResume();
                break;
        }
    }

    /**
     * 暂停图片请求
     */
    private void imagePause() {
        Fresco.getImagePipeline().pause();
    }

    /**
     * 恢复图片请求
     */
    private void imageResume() {
        Fresco.getImagePipeline().resume();
    }
}
